<template>
  <div :class="className" :style="{ 'height': '100%', 'width': '100%' }" />
</template>

<script>
import * as echarts from 'echarts'
import resize from '../../views/dashboard/mixins/resize';
require('echarts/theme/macarons') // echarts theme

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOption(this.chartData)
    },
    setOption({ xdata,totalCharge,totalElectricPowerConsumption,totalPowerGeneration } = {}) {
      this.chart.setOption({
        color: ['#80FFA5','#FFBF00' ,'#37A2FF','#00DDFF' ],
        grid: {
          top: 70,
          left: 10,
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          left: 'top',
          padding:[0,0,0,0],
          textStyle:{
            color:"#fff"
          }
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: true,//不从原点开始
            axisLabel:{color:'#fff'},
            data: xdata
          }
        ],
        yAxis: [
          {
            type: 'value',
            name:"kwh",
            scale: true,
            nameTextStyle: {
              color: "#fff",
              padding:[0,0,0,-10],
            },
            axisLabel:{color:'#fff'},
            splitLine: {
              show: true,
              lineStyle: {
                type:'dashed'
              }
            }
          }
        ],
        series: [
          {
            name: this.$t("index.totalChargingCapacity"),
            type: 'line',
            smooth: 0.5,
            lineStyle: {
              width: 2
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.7,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(128, 255, 165,0.4)'
                },
                {
                  offset: 0.7,
                  color: 'rgba(128, 255, 165,0.0)'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: totalCharge
          },
          {
            name: this.$t("index.totalElectricityConsumption"),
            type: 'line',
            smooth: true,
            lineStyle: {
              width: 2
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.4,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(255, 191, 0,1)'
                },
                {
                  offset: 0.7,
                  color: 'rgba(224, 62, 76,0)'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: totalElectricPowerConsumption
          },
          {
            name: this.$t("index.totalPowerGeneration"),
            type: 'line',
            smooth: true,
            lineStyle: {
              width: 2
            },
            showSymbol: false,
            areaStyle: {
              opacity: 0.4,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgba(55, 162, 255,1)'
                },
                {
                  offset: 0.7,
                  color: 'rgba(116, 21, 219,0)'
                }
              ])
            },
            emphasis: {
              focus: 'series'
            },
            data: totalPowerGeneration
          },
          // {
          //   name: 'Line 5',
          //   type: 'line',
          //   smooth: true,
          //   lineStyle: {
          //     width: 2
          //   },
          //   showSymbol: false,
          //   label: {
          //     show: true,
          //     position: 'top'
          //   },
          //   areaStyle: {
          //     opacity: 0.4,
          //     color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          //       {
          //         offset: 0,
          //         color: 'rgba(255, 191, 0,1)'
          //       },
          //       {
          //         offset: 0.7,
          //         color: 'rgba(224, 62, 76,0)'
          //       }
          //     ])
          //   },
          //   emphasis: {
          //     focus: 'series'
          //   },
          //   data: [220, 302, 181, 234, 210, 290, 150]
          // }
        ]
      })
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOption(val)
      }
    }
  },
}
</script>
